/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.fileupload.bean;

import com.hyts.fileupload.constant.FileUploadConstants;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.fileupload.configuration
 * @author:LiBo/Alex
 * @create-date:2020-05-07 10:47
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Data
@ConfigurationProperties(prefix ="upload")
public class FileUploadProperties {

    /**
     * 获取暂存的位置地址-系统自定义配置
     */
    private Map<String,String> locations;

    /**
     * 单次文件上传的文件大小（文件级别）
     */
    private Long maxFileSize;

    /**
     * 单次上传文件的总体大小（请求级别）
     */
    private Long maxRequestSize;

    /**
     * 获取根路径目录地址
     * @return
     */
    public String getBasePath(){
        String os = System.getProperty("os.name");
        locations = Optional.ofNullable(getLocations()).orElse(new HashMap<>(2));
        if(StringUtils.isNotBlank(os) && os.startsWith("win")){
            return StringUtils.defaultIfEmpty(getLocations().get("windows"), FileUploadConstants.DEFAULT_BASE_PATH);
        }else{
            return StringUtils.defaultIfEmpty(getLocations().get("linux"), FileUploadConstants.DEFAULT_BASE_PATH);
        }
    }

    public static void main(String[] args){
        System.out.println(System.getProperty("user.home"));
    }

}
